/*
 * CanvasStory.cpp
 *
 *  Created on: 2013-1-26
 *      Author: heqiu
 */

#include "Global.h"

void story_cycle(int32_t iXUnit, int32_t iYUnit, void * pData) {
	CanvasStory * pCanvasStory = (CanvasStory*) pData;
	GuudPoint gpScreen;
	GuudPoint gpUnit;
	gpUnit.m_iX = iXUnit;
	gpUnit.m_iY = iYUnit;
	pCanvasStory->transformToScreen(gpUnit, gpScreen);
	GuudPoint gp;
	gp.m_iX = gpScreen.m_iX * pCanvasStory->m_uiUnitPixel * 2
			- pCanvasStory->m_gpScreenPosPixel.m_iX;
	gp.m_iY = gpScreen.m_iY * pCanvasStory->m_uiUnitPixel / 2
			- pCanvasStory->m_gpScreenPosPixel.m_iY;
	//g_GlobalData.m_pGuudScreen->drawCoordinate(gp, g_GlobalData.m_GuudFont);
	string sCoordinate = "(" + GuudComm::getIntStr(gpUnit.m_iX) + ","
			+ GuudComm::getIntStr(gpUnit.m_iY) + ")";
	g_GlobalData.m_pGuudScreen->drawStr(gp, g_GlobalData.m_GuudFont,
			sCoordinate.c_str());
	/*
	 g_GlobalData.m_GuudLog.log("no=%d x=%u y=%u xs=%d ys=%d",
	 pCanvasStory->m_iStoryNo, uiXUnit, uiYUnit, gpScreen.m_iX,
	 gpScreen.m_iY);
	 */
}
void story_cycle_screen(GuudPoint & gpUnit, GuudPoint & gpScreen,
		void * pData) {
	CanvasStory * pCanvasStory = (CanvasStory*) pData;
	GuudPoint gp;
	gp.m_iX = gpScreen.m_iX * pCanvasStory->m_uiUnitPixel * 2
			- pCanvasStory->m_gpScreenPosPixel.m_iX;
	gp.m_iY = gpScreen.m_iY * pCanvasStory->m_uiUnitPixel / 2
			- pCanvasStory->m_gpScreenPosPixel.m_iY;
	//g_GlobalData.m_pGuudScreen->drawCoordinate(gp, g_GlobalData.m_GuudFont);
	string sCoordinate = "(" + GuudComm::getIntStr(gpUnit.m_iX) + ","
			+ GuudComm::getIntStr(gpUnit.m_iY) + ")";
	g_GlobalData.m_pGuudScreen->drawStr(gp, g_GlobalData.m_GuudFont,
			sCoordinate.c_str());
	GuudColor color;
	color.setColor(0, 255, 0);
	GuudPoint gpBegin = gp;
	gpBegin.m_iX += pCanvasStory->m_uiUnitPixel;
	gpBegin.m_iY -= pCanvasStory->m_uiUnitPixel;
	GuudPoint gpEnd = gpBegin;
	gpEnd.m_iX += pCanvasStory->m_uiUnitPixel * 2;
	gpEnd.m_iY += pCanvasStory->m_uiUnitPixel * 1;
	g_GlobalData.m_pGuudScreen->drawLine(gpBegin, gpEnd, color);
	gpEnd.m_iX -= pCanvasStory->m_uiUnitPixel * 4;
	g_GlobalData.m_pGuudScreen->drawLine(gpBegin, gpEnd, color);
	/*
	 g_GlobalData.m_GuudLog.log("no=%d x=%d y=%d xs=%d ys=%d",
	 pCanvasStory->m_iStoryNo, gpUnit.m_iX, gpUnit.m_iY, gpScreen.m_iX,
	 gpScreen.m_iY);
	 */
	GuudBuildingPtr pBuilding = pCanvasStory->getBuilding(gpUnit.m_iX,
			gpUnit.m_iY);
	if (pBuilding != NULL) {
		gp.m_iX = pBuilding->m_gpPosPixel.m_iX
				- pCanvasStory->m_gpScreenPosPixel.m_iX - 75;
		gp.m_iY = pBuilding->m_gpPosPixel.m_iY
				- pCanvasStory->m_gpScreenPosPixel.m_iY - 175;
		g_GlobalData.m_pGuudScreen->drawButton(gp, *pBuilding);
	}
}
CanvasStory::CanvasStory(uint32_t uiShortUnit, uint32_t uiLongUnit,
		uint32_t uiUnitPixel) :
		GuudMap(uiShortUnit, uiLongUnit, uiUnitPixel) {
	m_iStoryNo = 0;
}

void CanvasStory::draw() {
	//循环打印坐标点
	//cycleAll(story_cycle, this);
	m_screen_rect.x = m_gpScreenPosPixel.m_iX;
	m_screen_rect.y = m_gpScreenPosPixel.m_iY;
	cycleRect(m_screen_rect, story_cycle_screen, this);

	GuudBuilding * pBuilding =
			((CanvasControl*) g_GlobalData.m_pGuudCanvasControl)->m_p_select_building;
	if (pBuilding != NULL) {
		GuudPoint gp(
				g_GlobalData.m_GameListener.m_iMouseX + m_gpScreenPosPixel.m_iX
						- 25,
				g_GlobalData.m_GameListener.m_iMouseY + m_gpScreenPosPixel.m_iY
						+ 25);
		GuudPoint gpUnit;
		transformPixelToUnit(gp, gpUnit);
		pBuilding->m_gpPosUnit.m_iX = gpUnit.m_iX;
		pBuilding->m_gpPosUnit.m_iY = gpUnit.m_iY - 1 + pBuilding->m_iYLenUnit;

		GuudPoint gpScreen;
		transformToScreen(gpUnit, gpScreen);
		gp.m_iX = gpScreen.m_iX * m_uiUnitPixel * 2 - m_gpScreenPosPixel.m_iX;
		gp.m_iY = gpScreen.m_iY * m_uiUnitPixel / 2 - m_gpScreenPosPixel.m_iY;
		{
			GuudColor color;
			if (this->testBuilding(*pBuilding)) {
				color.setColor(0, 0, 255);
			} else {
				color.setColor(255, 0, 0);
			}

			GuudPoint gpA = gp;
			gpA.m_iX += m_uiUnitPixel * 3;
			GuudPoint gpB = gpA;
			gpB.m_iX -= m_uiUnitPixel * 2 * pBuilding->m_iYLenUnit;
			gpB.m_iY += m_uiUnitPixel * pBuilding->m_iYLenUnit;
			GuudPoint gpC = gpB;
			gpC.m_iX -= m_uiUnitPixel * 2 * pBuilding->m_iXLenUnit;
			gpC.m_iY -= m_uiUnitPixel * pBuilding->m_iXLenUnit;
			GuudPoint gpD = gpC;
			gpD.m_iX += m_uiUnitPixel * 2 * pBuilding->m_iYLenUnit;
			gpD.m_iY -= m_uiUnitPixel * pBuilding->m_iYLenUnit;
			g_GlobalData.m_pGuudScreen->drawLine(gpA, gpB, color);
			g_GlobalData.m_pGuudScreen->drawLine(gpB, gpC, color);
			g_GlobalData.m_pGuudScreen->drawLine(gpC, gpD, color);
			g_GlobalData.m_pGuudScreen->drawLine(gpD, gpA, color);
			gpA.m_iY++;
			gpB.m_iY++;
			gpC.m_iY++;
			gpD.m_iY++;
			g_GlobalData.m_pGuudScreen->drawLine(gpA, gpB, color);
			g_GlobalData.m_pGuudScreen->drawLine(gpB, gpC, color);
			g_GlobalData.m_pGuudScreen->drawLine(gpC, gpD, color);
			g_GlobalData.m_pGuudScreen->drawLine(gpD, gpA, color);
		}
		//g_GlobalData.m_pGuudScreen->drawStr(gp, g_GlobalData.m_GuudFont, "xxx");
		/*
		 GuudPoint gpScreen;
		 transformToScreen(gpUnit, gpScreen);
		 char buf[64];
		 sprintf(buf, "unit:[%d,%d]screen:[%d,%d]", gpUnit.m_iX, gpUnit.m_iY,
		 gpScreen.m_iX, gpScreen.m_iY);
		 gp.m_iX = g_GlobalData.m_GameListener.m_iMouseX;
		 gp.m_iY = g_GlobalData.m_GameListener.m_iMouseY;
		 g_GlobalData.m_pGuudScreen->drawStr(gp, g_GlobalData.m_GuudFont, buf);
		 */

	}
}
void CanvasStory::init() {
	string sStory = "story-" + GuudComm::getIntStr(m_iStoryNo);
	int32_t iXPixel = g_GlobalData.m_config_save.getKKValueInt32(sStory.c_str(),
			"x-pixel");
	int32_t iYPixel = g_GlobalData.m_config_save.getKKValueInt32(sStory.c_str(),
			"y-pixel");
	m_gpScreenPosPixel.m_iX = iXPixel;
	m_gpScreenPosPixel.m_iY = iYPixel;
	m_screen_rect.w = g_GlobalData.m_pGuudScreen->m_uiWidthPixel;
	m_screen_rect.h = g_GlobalData.m_pGuudScreen->m_uiHeightPixel;
	m_bListen = true;
}
void CanvasStory::handleEvent(GuudEvent &stEvent) {
	if (m_bListen) {
		m_MovingEvent.handleEvent(this, stEvent);
		if (stEvent.type == SDL_MOUSEBUTTONUP) {
			GuudBuilding * pBuilding =
					((CanvasControl*) g_GlobalData.m_pGuudCanvasControl)->m_p_select_building;
			if (pBuilding != NULL) {
				//addBuilding();
				if (this->testBuilding(*pBuilding)) {
					GuudBuilding * pTmp = new GuudBuilding(*pBuilding);
					GuudPoint gpUnit(pTmp->m_gpPosUnit);
					GuudPoint gpPixel;
					transformUnitToPixel(gpUnit, gpPixel);
					pTmp->m_uiID = g_GlobalData.m_BuildingManager.getNewId();
					pTmp->m_gpPosPixel = gpPixel;
					g_GlobalData.m_BuildingManager.addBuilding(pTmp->m_uiID,
							pTmp);
					this->setBuilding(*pTmp);
				}
			}
		}
	}
}
